#include <iostream>
#include <string>
#include <list>
#include <stack>
#include <vector>
using namespace std;

class Solution
{
public:
    string clearStars(string s)
    {
        list<char> str;
        vector<stack<list<char>::iterator>> charStacks(26);
        for (auto c : s)
        {
            if (c == '*')
            {
                for (stack<list<char>::iterator> &stack : charStacks)
                {
                    if (stack.empty())
                    {
                        continue;
                    }
                    str.erase(stack.top());
                    stack.pop();
                    break;
                }
            }
            else
            {
                auto it = str.insert(str.end(), c);
                charStacks[c - 'a'].push(it);
            }
        }
        return string(str.begin(), str.end());
    }
};

int main()
{
    cout << "Code3170" << endl;
    Solution s;
    cout << s.clearStars("aaba*") << endl;
    return 0;
}